layui.use(['layer', 'jquery'], function () {
    var layer = layui.layer;
    var $ = layui.$;

    $('.pass-tip').on('click', function () {
        layer.msg("请联系管理员", { icon: 8, time: 1000 });
    });
    $('#reg-btn').on('click', function () {
        $("#register-form").show();
        $("#login-form").hide();
    });
    $('#log-btn').on('click', function () {
        $("#login-form").show();
        $("#register-form").hide();
    });

    //绑定登录按钮
    $('#loginBtn').on('click', function () {
        if ($("#userName").val() == '' || $("#password").val() == '') {
            layer.msg("请输入用户名或密码", { offset: 't', icon: 5 });
            return false;
        }

        var params = $("#loginForm").serialize();
        $.ajax({
            url: 'http://localhost:8080/user/login',
            type: 'post',
            data: params,
            success: function (vo) {
                if (vo.code == 401) {
                    layer.msg(vo.message, { offset: 't', icon: 5 });
                } else if (vo.code == 200) {
                    /**
                     * 登录成功则添加当前用户的一条日志记录
                     */
                    let formData = {
                        userId: vo.data.userId,
                        userName: vo.data.userName,
                        realName: vo.data.realName,
                        content: '登录了阿里迪里考试系统'
                    }
                    $.ajax({
                        url: 'http://localhost:8080/log/add',
                        type: 'post',
                        data: formData,
                        success: function (vo) {

                        }
                    });

                    /**
                     * 查询当前登录用户的未读消息数
                     */
                    $.ajax({
                        url: 'http://localhost:8080/message/selectAll',
                        data: { receiveUserId: vo.data.userId,status:1},
                        type: 'get',
                        success: function (vo2) {
                            if (vo2.code == 200) {
                                sessionStorage.setItem("unreadNum",vo2.data.length);
                                sessionStorage.setItem("user", JSON.stringify(vo.data));
                                location.href = '../home/index.html';
                            }
                        }
                    });

                }
            }
        });
    });



    // 注册
    $('#regBtn').on('click', function () {

        let pass1 = document.getElementById("password1").value;
        let pass2 = document.getElementById("password2").value;
        if (pass1 != pass2) {
            layer.msg("密码不一致，请重新输入", { offset: 't', icon: 5 });
            return;
        }
        if ($("#userName2").val() == '' || $("#password1").val() == '' || $("#userLevel").val() == '') {
            layer.msg("请填入完整信息", { offset: 't', icon: 5 });
            return;
        }

        let p = $("#regForm").serialize();
        console.log(p);
        $.ajax({
            url: 'http://localhost:8080/user/reg',
            type: 'get',
            data: p,
            success: function (vo) {
                if (vo.code == 401) {
                    layer.msg(vo.message, { offset: 't', icon: 5 });
                } else if (vo.code == 200) {
                    layer.msg("注册成功！", { offset: 't', icon: 6 });

                    $("#userName2").val("");
                    $("#password1").val("");
                    $("#password2").val("");
                    $("#login-form").show();
                    $("#register-form").hide();
                }
            }
        });
    });
});